#!/bin/bash

. ~/bin/functions
is_installed gpg
is_installed dialog
is_installed mktemp
is_installed basename

if [ -f ~/.walletrc ]; then
    . ~/.walletrc
fi

READONLY=${READONLY:=0}

if [ -z "$VISUAL" ]; then
    VISUAL=vi
fi
if [ -z "$WALLET_PAGER" ]; then
    WALLET_PAGER=less
fi

EDIT_PWFILE=0
while getopts 'ec:' OPTION
do
  case $OPTION in
    e) EDIT_PWFILE=1;;
    c) WALLET_FILENAME="$OPTARG";;
    ?) printf "usage: %s [ -e ] [ -c encrypted_file ]\n" \
        $( basename $0 ) >&2
       exit 1
       ;;
  esac
done
shift $(($OPTIND - 1))

if [ $READONLY -ne 0 ];
    then EDIT_PWFILE=0
fi

if [ -z "$WALLET_FILENAME" ]; then
    echo "need the encrypted file specified by WALLET_FILENAME"
    echo "(in ~/.walletrc or the envariable) or with the -c option"
    exit 2
fi

if [ ! -f $WALLET_FILENAME ]; then
    echo "$WALLET_FILENAME doesn't exist--attempting to create..."
    echo "(you'll need to give gpg a master password)"
    mkdir -p $( dirname $WALLET_FILENAME )
    TEMPFILE=$( mktemp /tmp/wallet.XXXXXX )
    gpg -c -o $WALLET_FILENAME $TEMPFILE
    rm -f $TEMPFILE
    EDIT_PWFILE=1
fi

if [ $EDIT_PWFILE -eq 1 ]; then
    is_installed $VISUAL
fi

# prompt the user for the password
PASSWORD=$( dialog --stdout --backtitle "Password Wallet" \
    --title "Master Password" --clear --passwordbox \
    "Please provide the master password." 8 40 )
if [ $? -ne 0 ]; then
    echo "Failed to acquire master password"
    exit 4
fi
if [ -z $PASSWORD ]; then
    echo "Password is required"
    exit 8
fi

# if we're not editing the file, just display it and quit
if [ $EDIT_PWFILE -eq 0 ]; then
    echo $PASSWORD | gpg --decrypt --passphrase-fd 0 \
        $WALLET_FILENAME | $WALLET_PAGER
    clear
    exit 0
fi

# set up the directory in which the unencrypted wallet file
# will be edited
TMPDIR=$( mktemp -d /tmp/wallet.XXXXXX )
CLEARTEXT_WALLET_FILENAME=$TMPDIR/wallet

# try to ensure that cleartext wallet file is deleted, even after
# unexpected terminations
trap "{ rm -rf $TMPDIR; }" 0 1 2 5 15

# decrypt the password wallet--an error here probably means that the
# user typed the wrong password to decrypt the wallet
echo $PASSWORD | gpg -o $CLEARTEXT_WALLET_FILENAME \
    --passphrase-fd 0 \
    $WALLET_FILENAME &> /dev/null
case $? in
    0)
        # decryption succeeded, so open the wallet in the editor and
        # then re-encrypt it when the editor closes
        mv $WALLET_FILENAME ${WALLET_FILENAME}.bak
        $VISUAL $CLEARTEXT_WALLET_FILENAME 2> /dev/null
        echo $PASSWORD | gpg -c -o $WALLET_FILENAME \
            --passphrase-fd 0 \
            $CLEARTEXT_WALLET_FILENAME &> /dev/null
        if [ $? -eq 0 ]; then
            clear
        else
            LAST_RESORT_FILENAME=$( mktemp ~/wallet.XXXXXX )
            cp $CLEARTEXT_WALLET_FILENAME $LAST_RESORT_FILENAME
            chmod 600 $LAST_RESORT_FILENAME
            echo "gpg failed to enrypt your password wallet: I have"
            echo "tried to put a CLEARTEXT copy of your wallet at"
            echo $LAST_RESORT_FILENAME
            exit 16
        fi
        exit 0;;
    ?)
        echo "error condition detected (invalid password?)"
        exit 32;;
esac
